@page "/audit/reports/create"
@using SDK.Authorization
@using AyBorg.Web.Pages.Audit.Shared
@attribute [Authorize(Roles = $"{Roles.Administrator}, {Roles.Auditor}")]

<PageTitle>Audit Report</PageTitle>

<div class="page-loading-bar">
    <MudProgressLinear Color="Color.Primary" Indeterminate="true" Class="my-0" hidden="@(!_isLoading)" />
</div>

@if (!_areChangesetsAvailable)
{
    <PagePlaceholder>
        <MudAlert Severity="Severity.Info" ContentAlignment="HorizontalAlignment.Center"><strong>No changesets</strong> available.</MudAlert>
    </PagePlaceholder>
}

<div hidden="@(_isFilterHidden || !_areChangesetsAvailable)">
    <MudToolBar Class="mud-full-width px-0">
        <MudDateRangePicker Label="Date Range" Editable="true" @bind-DateRange="_dateRange" Class="mr-5" />
        <MudSelect T="ServiceOption" Label="Services" MultiSelection="true" SelectAll="true"
            ToStringFunc="@_serviceSelectionConveter" @bind-SelectedValues="_selectedOptions" Class="mr-5">
            @foreach (var option in _selectableServiceOptions)
            {
                <MudSelectItem T="ServiceOption" Value="@option">@option.Label</MudSelectItem>
            }
        </MudSelect>
        <MudTooltip Text="Filter">
            <MudIconButton Icon="@Icons.Material.Filled.FilterList" Color="Color.Primary" OnClick="FilterClicked" />
        </MudTooltip>
        <MudTooltip Text="Compare">
            <MudIconButton Icon="@Icons.Material.Filled.Compare" Color="Color.Primary" OnClick="CompareClicked" />
        </MudTooltip>
    </MudToolBar>

    @if (!_filteredGroupedChangesets.Keys.Any())
    {
        <MudAlert Severity="Severity.Info" ContentAlignment="HorizontalAlignment.Center"><strong>No changesets</strong> to compare.</MudAlert>
    }

    @foreach (var service in _filteredGroupedChangesets.Keys)
    {
        <MudCard Class="mud-full-width rounded-lg mt-5">
            <MudCardHeader Class="pb-0">
                <CardHeaderAvatar>
                    <MudIcon Icon="@Icons.Material.Filled.TrackChanges" />
                </CardHeaderAvatar>
                <CardHeaderContent>
                    <MudText Typo="Typo.h6">@service.Label - Changesets</MudText>
                </CardHeaderContent>
            </MudCardHeader>
            <MudCardContent>
                <AuditChangesetTable @ref="_changesetTables[service]" Changesets="@_filteredGroupedChangesets[service]" />
            </MudCardContent>
        </MudCard>
    }
</div>

@if (_isFilterHidden)
{
<MudToolBar Class="mud-full-width px-0">
    <MudTooltip Text="Back">
        <MudFab StartIcon="@Icons.Material.Filled.ArrowBackIosNew" Color="Color.Primary" Size="Size.Small"
            OnClick="PreviewBackClicked" />
    </MudTooltip>
    <MudSpacer />
    <MudTooltip Text="Save report">
        <MudIconButton Icon="@Icons.Material.Filled.SaveAlt" Title="Save report" Color="Color.Primary"
            OnClick="@SaveClicked" />
    </MudTooltip>
</MudToolBar>
<CompareView SelectedChangesets="_selectedChangesets" Loaded="CompareViewLoaded" />
}
